package cn.org.eshow.webapp.action;

import cn.org.eshow.bean.query.BillQuery;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.model.Bill;
import cn.org.eshow.model.User;
import cn.org.eshow.service.BillManager;
import cn.org.eshow.webapp.action.response.BillResponse;
import cn.org.eshow.webapp.util.RenderUtil;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 账单API接口
 */
@AllowedMethods({"mine"})
public class BillAction extends ApiBaseAction<Bill> {

    private static final long serialVersionUID = 1L;

    @Autowired
    private BillManager billManager;

    private Bill bill;
    private List<Bill> bills = new ArrayList<Bill>();
    private BillQuery query = new BillQuery();

    /**
     * 搜索我的账单列表
     */
    public void mine() {
        User user = accessTokenManager.isValid(accessToken);
        if (user == null) {
            RenderUtil.expires();//用户信息过期
            return;
        }
        Page<Bill> page = billManager.search(query);
        bills = page.getDataList();
        if (bills.isEmpty()) {
            RenderUtil.noData();
            return;
        }
        List<BillResponse> responses = new ArrayList<BillResponse>();
        for (Bill obj : bills) {
            responses.add(new BillResponse(obj));
        }
        RenderUtil.page("获取成功", "bills", page, responses);
    }

    public Bill getBill() {
        return bill;
    }

    public void setBill(Bill bill) {
        this.bill = bill;
    }

    public List<Bill> getBills() {
        return bills;
    }

    public void setBills(List<Bill> bills) {
        this.bills = bills;
    }

    public BillQuery getQuery() {
        return query;
    }

    public void setQuery(BillQuery query) {
        this.query = query;
    }
}